package com.gxar.marketdelivery.domain.repo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxar.marketdelivery.infra.dao.entity.DeliveryPlanSubscriberEntity;
import com.gxar.marketdelivery.infra.dao.mapper.DeliveryPlanSubscriberMapper;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import com.gxar.marketdelivery.shared.consts.DBConsts;
import com.gxar.marketdelivery.shared.utils.AssertUtils;
import com.gxar.marketdelivery.shared.utils.DBUtils;
import com.gxar.marketdelivery.shared.utils.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author nice
 * @version 1.0
 * @created 2023/7/23.
 */
@Service
public class DeliveryPlanSubscriberRepository {

    @Autowired
    protected DeliveryPlanSubscriberMapper mapper;

    public void create(DeliveryPlanSubscriberEntity entity) {
        entity.setGmtModify(DateTimeUtils.now());
        int ret = mapper.insert(entity);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "投放计划订阅失败");
    }

    public void delete(Long id) {
        UpdateWrapper<DeliveryPlanSubscriberEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(DeliveryPlanSubscriberEntity.FIELD_STATUS, DBConsts.STATUS_UNAVAILABLE);
        updateWrapper.set(DeliveryPlanSubscriberEntity.FIELD_GMT_MODIFY, DateTimeUtils.now());
        updateWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_ID, id);
        int ret = mapper.update(getById(id), updateWrapper);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "投放计划取消订阅失败");
    }

    public DeliveryPlanSubscriberEntity getSubscribe(Long deliveryPlanId, String mis) {
        QueryWrapper<DeliveryPlanSubscriberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.eq(DeliveryPlanSubscriberEntity.DELIVERY_PLAN_ID, deliveryPlanId);
        queryWrapper.eq(DeliveryPlanSubscriberEntity.SUBSCRIBER_MIS, mis);
        return mapper.selectOne(queryWrapper);
    }

    public List<DeliveryPlanSubscriberEntity> getByDeliveryPlanId(Long deliveryPlanId) {
        QueryWrapper<DeliveryPlanSubscriberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.eq(DeliveryPlanSubscriberEntity.DELIVERY_PLAN_ID, deliveryPlanId);
        return mapper.selectList(queryWrapper);
    }

    public List<DeliveryPlanSubscriberEntity> getByDeliveryPlanIds(List<Long> deliveryPlanIds) {
        QueryWrapper<DeliveryPlanSubscriberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.in(DeliveryPlanSubscriberEntity.DELIVERY_PLAN_ID, deliveryPlanIds);
        return mapper.selectList(queryWrapper);
    }

    public List<DeliveryPlanSubscriberEntity> getByDeliveryPlanIds(List<Long> deliveryPlanIds, String mis) {
        QueryWrapper<DeliveryPlanSubscriberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.in(DeliveryPlanSubscriberEntity.DELIVERY_PLAN_ID, deliveryPlanIds);
        queryWrapper.eq(DeliveryPlanSubscriberEntity.SUBSCRIBER_MIS, mis);
        return mapper.selectList(queryWrapper);
    }

    public DeliveryPlanSubscriberEntity getById(Long id) {
        QueryWrapper<DeliveryPlanSubscriberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        queryWrapper.eq(DeliveryPlanSubscriberEntity.FIELD_ID, id);
        return mapper.selectOne(queryWrapper);
    }
}
